
package com.j2eefast.produce.pms.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.framework.code.service.SerialService;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import com.j2eefast.produce.pms.entity.PmsEntrustEntity;
import com.j2eefast.produce.pms.entity.PmsRegisterEntrustEntity;
import com.j2eefast.produce.pms.mapper.PmsEntrustMapper;
import com.j2eefast.produce.pms.mapper.PmsRegisterEntrustMapper;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>委托登记</p>
 *
 * @author: summer
 * @date: 2021-05-30 16:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */
@Service
@Transactional(readOnly = true)
public class PmsEntrustService extends ServiceImpl<PmsEntrustMapper, PmsEntrustEntity> {

	@Resource
	protected PmsEntrustMapper pmsEntrustMapper;
	@Resource
	@Lazy
	private SerialService pmsSerialService;

	/**
	 * 页面展示查询翻页
	 */
	public PageUtil findPage(Map<String, Object> params) {
//		String sampleCode = (String) params.get("sampleCode");
//		String sampleType = (String) params.get("sampleType");

		String scene = (String) params.get("scene");
		Page<PmsEntrustEntity> page = pmsEntrustMapper.findPage(
				new Query<PmsEntrustEntity>(params).getPage(),
				StrUtil.nullToDefault(scene,""),
//				StrUtil.nullToDefault(sampleType,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteByIds(Long[] ids) {

		return pmsEntrustMapper.deleteByIds(ids);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean updateCheck(Long id,int checkStatus) {
//		Long userId= UserUtils.getUserInfo().getId();
		String updateBy=UserUtils.getUserInfo().getName();
		return pmsEntrustMapper.updateCheck(id ,checkStatus,updateBy);

	}
	@Transactional(rollbackFor = Exception.class)
	public boolean issuedCheck(Long id,int checkStatus) {
//		Long userId= UserUtils.getUserInfo().getId();
		String updateBy=UserUtils.getUserInfo().getName();
		return pmsEntrustMapper.issuedCheck(id ,checkStatus,updateBy);

	}

	/**
	 * 添加
	 *
	 */
	@Transactional(rollbackFor = Exception.class)
	public boolean saves(PmsEntrustEntity taskFrom){
//		Long userId= UserUtils.getUserInfo().getId();
		Date date = new Date();//创建一个时间对象，获取到当前的时间
		taskFrom.setUpdateTime(date);
		taskFrom.setCreateTime(date);
		String userName=UserUtils.getUserInfo().getName();
		taskFrom.setCreateBy(userName);
		taskFrom.setUpdateBy(userName);
		PmsEntrustEntity pmsTemplateEntity = pmsEntrustMapper.selectRegister();
		Long total=0L;
		if(pmsTemplateEntity!=null && pmsTemplateEntity.getSampleTotal()!=null){
			total=pmsTemplateEntity.getSampleTotal();
		}
		Map<String ,String> participant=pmsSerialService.formatTitle(total,"默认序列号");//编号
		taskFrom.setSampleCode(participant.get("title"));
		taskFrom.setSampleTotal(Long.parseLong(participant.get("total")));
		return this.save(taskFrom);

	}



	public Map<String,Object> codes(){
		HashMap map = new HashMap<String, Object>();
		PmsEntrustEntity entrustEntity = pmsEntrustMapper.selectRegister();
		Long total=0L;
		if(entrustEntity!=null && entrustEntity.getEntrustTotal()!=null){
			total=entrustEntity.getEntrustTotal();
		}
		Map<String ,String> participant=pmsSerialService.formatTitle(total,"默认序列号");//编号
//		taskFrom.setEntrustCode(participant.get("title"));
//		taskFrom.setTotal(Long.parseLong(participant.get("total")));
		String title=participant.get("title");
		System.out.println("3333");
		System.out.println(title);
		System.out.println("3333");
		total=Long.parseLong(participant.get("total"));
		map.put("title",title);
		map.put("total",total);
		return map;
	}

	@Transactional(rollbackFor = Exception.class)
	public boolean updateTask(PmsEntrustEntity pmsEntrustEntity ){

		return pmsEntrustMapper.updateTask(pmsEntrustEntity);

	}

	public PmsEntrustEntity selectEntrust(String entrustCode){
		return pmsEntrustMapper.selectEntrust(entrustCode);
	}


}
